package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.Util.PageResult;
import com.story.common.constant.factory.PageFactory;
import com.story.common.persistence.model.CategoryEntity;
import com.story.common.persistence.model.VedioDetail;
import com.story.core.base.controller.BaseController;
import com.story.modular.app.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告管理分类控制器
 *
 * @author fengshuonan
 * @Date 2019-12-27 11:37:32
 */
@Controller
@RequestMapping("/category")
public class GgCategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    private String PREFIX = "/app/category/";

    /**
     * 跳转到广告管理分类首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }

    /**
     * 跳转到添加广告管理分类
     */
    @RequestMapping("/category_add")
    public String categoryAdd() {
        return PREFIX + "category_add.html";
    }

    /**
     * 跳转到修改广告管理分类
     */
    @RequestMapping("/category_update")
    public String categoryUpdate(@PathVariable Integer categoryId, Model model) {
        return PREFIX + "category_edit.html";
    }
    @RequestMapping(value = "/querylist")
    @ResponseBody
    public Object queryList(){
        Map<String,Object> map=new HashMap<String,Object>();

        return  categoryService.queryList(map);
    }
    /**
     * 获取广告管理分类列表
     */
    @ApiOperation("广告分类列表 name：广告名称 page：当前页  limit：每页记录数")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(String name,@RequestParam(required=false,defaultValue="1") int page,
                       @RequestParam(required=false,defaultValue="10") int limit) {
        Map<String,Object> map=new HashMap<String,Object>();
        if (!"".equals(name) && name !=null){
            map.put("name",name);
        }
        Page<CategoryEntity> page1 = new Page<CategoryEntity>();

        //page.setRecords(categoryService.queryPageList(page,condition));

        page1.setCurrent(page);
        page1.setSize(limit);
        PageResult pageResult=new PageResult();
        pageResult.setData(categoryService.queryPageList(page1,name));
        pageResult.setCode("0");
        int total=page1.getTotal();
        System.out.println(total);
        pageResult.setCount(page1.getTotal());

        pageResult.setMsg("广告分类列表");
        return pageResult;
    }

    /**
     * 新增广告管理分类
     */
    @ApiOperation("添加广告分类")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(CategoryEntity entity) {
        System.out.println(entity.toString());
        categoryService.save(entity);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除广告管理分类
     */
    @ApiOperation("删除广告分类 删除id")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam(value = "ids") String ids) {
        System.out.println(ids);
        categoryService.delete(Integer.parseInt(ids));
        return SUCCESS_TIP;
    }


    /**
     * 修改广告管理分类
     */
    @ApiOperation("修改广告分类")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(CategoryEntity categoryEntity) {
        System.out.println(categoryEntity.toString());
        categoryService.update(categoryEntity);
        return super.SUCCESS_TIP;
    }

    /**
     * 广告管理分类详情
     */
    @ApiOperation("广告分类详情")
    @RequestMapping(value = "/detail",method = RequestMethod.POST)
    @ResponseBody
    public Object detail(Integer id) {
        return categoryService.queryObject(id);
    }


}
